package com.wiserun.RestFul.service;

import com.wiserun.RestFul.mapper.PncsUserMapper;
import com.wiserun.RestFul.model.User;
import com.wiserun.RestFul.model.UserDeptVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class PncsUserService {
    @Autowired
    private PncsUserMapper userMapper;

    //增加单个用户
    public int addUser(User user) {
        int num = 0;
        if (user != null) {
            num = userMapper.addUser(user);
        }
        return num;
    }

    //批量增加用户
    public HashMap<String,Integer> addUsers(List<User> users) {
        int num_add = 0;
        int num_update = 0;
        int sum_add = 0;
        int sum_update = 0;
        HashMap<String,Integer> counts = new HashMap<>();
        if (users != null) {
            for (User user : users) {
                User user1 = this.getUser(user.getUserID());
                if (user1 == null) {//用户不重复
                    num_add = userMapper.addUser(user);
                    sum_add += num_add;
                    continue;
                }
                num_update = userMapper.updateUser(user);
                sum_update+=num_update;
            }
        }
        counts.put("sum_add",sum_add);
        counts.put("sum_update",sum_update);
        return counts;
    }

    /**
     * 查询用户是否已经存在
     * 存在返回数据集
     * 不存在返回null
     */
    public User getUser(String userID) {
        return userMapper.queryByUserId(userID);
    }

    /**
     * 查询所有用户
     */
    public List<UserDeptVO> getAllUsers(){
        return userMapper.queryAllUsers();
    }
}
